#include"processbar.h"

const char arr[]="\\|-/";
int len = sizeof(arr)/sizeof(arr[0]) - 1; 

void process()  //version1
{
    char buffer[Size];
    memset(buffer, '\0', sizeof(buffer));
    int cnt = 0; 
    buffer[0] = Head;
    while(cnt <= 100)
    {
        printf("[%-100s][%3d%%][%c]\r", buffer, cnt, arr[cnt%len]);
        fflush(stdout);
        buffer[cnt++] = Body;
        if(cnt < 100)   //注意：cnt!=100,原因：buffer[100]= >,为101位字符，尽管还会进循环让buffer[100]= =,但此时cnt=101，不会进入循环进行打印出上次修改的字符
             buffer[cnt] = Head;
        usleep(100000);

    }
    printf("\n");
}


char buffer[Size] = {0};  //version2

void process_flush(double rate)
{
    static int cnt = 0; //光标的转动快慢不随着下载量多少变化，只要在进行下载就在均匀的转动
    if(rate <= 1.0)
        buffer[0] = Head; //起始位置
    printf("[\033[0;43;31m]%-100s\033[0m][%.1lf%%][%c]\r", buffer, rate, arr[cnt%len]);
    fflush(stdout);  //刷新缓冲区
    buffer[(int)rate] = Body; //下载量只要大于该等于它的整数，该位就会被更新
    if((int)rate + 1 < 100.0)  //注意:(int)rate+1,防止出现最后一位为>,不是为=
         buffer[(int)rate + 1] = Head;
    cnt++;
}
